//
//  SSTabBarItem.swift
//  weibo
//
//  Created by 邵帅 on 16/7/6.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

class SSTabBarItem: UITabBarItem {
    
    override var badgeValue: String?{
        
        didSet{
            
            if badgeValue == nil {
                
                return
            }
            
            let tabBarController: UITabBarController = (valueForKey("_target")as? UITabBarController)!
            
            
            for childTabBar in tabBarController.tabBar.subviews{
                
                if childTabBar.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    
                    for childTabBarButton in childTabBar.subviews{
                        
                        if childTabBarButton.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            
                            for childBadgeView in childTabBarButton.subviews{
                                
                                if childBadgeView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    
                                    var count: UInt32 = 0
                                    
                                    let result = class_copyIvarList(NSClassFromString("_UIBadgeBackground"), &count)
                                    
                                    for i in 0..<count{
                                        
                                        let ivar = result[Int(i)]
                                        
                                        let cname = ivar_getName(ivar)
                                        
                                        let name = String(CString: cname, encoding: NSUTF8StringEncoding)
                                        
                                        print(name)
                                    }
                                    
                                    
                                    childBadgeView.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                    
                                }
                                
                            }
                        }
                        
                    }
                    
                }
                
            }
            
        }
      
    }
    

}
